import config from '../../config/index.js'

function request(method, url, data, header = {}) {
	return new Promise((resolve, reject) => {
		uni.request({
			data,
			method,
			header,
			url,
			success: function(res) {
				// if (res.data.code === 200) {
				// 	resolve(res.data)
				// } else {
				// 	reject(res.data)
				// }
        if (res.data.code) {
            const code = res.data.code.toString();
    
            if (code.includes('20')) {
                resolve(res.data);
            }
        }
        if (res.data.infocode == 10000) {
            resolve(res.data);
        }
        reject(res.data);
			}
		})
	})
}
// http get工具函数 获取数据
function get(url, data, header = {}) {
  const newUrl = config.host + url
	return request('GET', newUrl, data, header)
}

function get2(url, data, header = {}) {
  const newUrl = url
	return request('GET', newUrl, data, header)
}

function post(url, data, header = {
	// 'content-type': 'application/x-www-form-urlencoded',
  'content-type':'application/json; charset=utf-8'
}) {
  const newUrl = config.host + url
	return request('POST', newUrl, data, header)
}

function post2(url, data, header = {
	// 'content-type': 'application/x-www-form-urlencoded',
  'content-type':'application/json; charset=utf-8'
}) {
  const newUrl = url
	return request('POST', newUrl, data, header)
}

module.exports = {
	get,
  get2,
	post,
  post2
}